<?php
/**
 * Archive Message
 * Change the status to archived
 * 
 * @author Xavier VILAIN
 * @package #license-mailer#
 * 
 */

use Orx\Mailer\MailBox\Message;
$xview = Xview::instance();
$messageids = $xview->val('messageids');
if(!empty($messageids)) {
	// Load All Messages
	$nbNessages = 0;
	$messageids = explode(':',$messageids);
	foreach($messageids as $messageid) {
		if(empty($messageid)) {
			continue;
		}
		try {
			$message = \Fox\Dao::load(Message::className(),$messageid);
			$message->status = Message::STATUS_ARCHIVED;
			$message->prepareUpdate(Message::CHANGE_ARCHIVE);
			$message->update();
			$nbNessages++;
		} catch(Fox\Object\Exception\NotExist $e) {
			continue;
		} catch(Fox\Object\Exception\Save $e) {
			Fox\Message::Error($e);
		}
	}
	if($nbNessages > 0) {
		Fox\Message::Success(Msg(Orx\Mailer\Messages::BUNDLE,Orx\Mailer\Messages::X_MESSAGES_HAS_BEEN_ARCHIVED,$nbNessages));
	} elseif(!Fox\Message::errors()) {
		Fox\Message::Success(Msg(Orx\Mailer\Messages::BUNDLE,Orx\Mailer\Messages::X_MESSAGES_HAS_BEEN_ARCHIVED,'0'));
	}
	echo $xview->messages(true,true);
}